package demo.jdk.features.jdk6;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Ranchoz  2018/9/9 11:29
 **/

public class BasicScripting {


	public void greet() throws ScriptException {
		ScriptEngineManager manager = new ScriptEngineManager();
		//支持通过名称、文件扩展名、MIMEtype查找
		ScriptEngine engine = manager.getEngineByName("JavaScript");
//        engine = manager.getEngineByExtension("js");
//        engine = manager.getEngineByMimeType("text/javascript");
		if (engine == null) {
			throw new RuntimeException("找不到JavaScript语言执行引擎。");
		}
//		engine.eval("println('Hello!');");
		engine.eval("print('Hello!')");
	}



	public static void main(String[] args) {
		try {
			new BasicScripting().greet();
		} catch (ScriptException ex) {
			Logger.getLogger(BasicScripting.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
}
